﻿using SqlSugar;

namespace 表单特性
{
    internal class Program
    {
        static void Main(string[] args)
        {

            var persons = SqlHelper.Db.Queryable<Person>().Where(it => it.Name == "刘德华").ToList(); // 查询所有名字为Alice的用户列表  

            foreach (var user in persons)
            {
                Console.WriteLine(user);
            }

            var personById = SqlHelper.Db.Queryable<Person>().Single(it => it.Id == 1); // 根据ID查询用户记录

            Console.WriteLine(personById);
            Console.WriteLine("Hello, World!");
        }
    }

    [SugarTable("person")]
    public class Person
    {
        [SugarColumn(ColumnName = "id",IsPrimaryKey = true)]
        public long Id { get; set; }
        [SugarColumn(ColumnName = "age")]
        public int Age { get; set; }
        [SugarColumn(ColumnName = "name")]
        public string Name { get; set; }
        [SugarColumn(ColumnName = "sex")]
        public string Sex { get; set; }
        [SugarColumn(ColumnName = "addr")]
        public string Addr { get; set; }

        public override string? ToString()
        {
            return $"Id:{Id},Name:{Name},Age:{Age},Sex:{Sex},Addr:{Addr}";
        }
    }

    internal class SqlHelper
    {
        private static readonly string constr = "Data Source=.;Initial Catalog=test;User Id=sa;Password=root123;";

        public static SqlSugarClient Db { 
            get { 
                return new SqlSugarClient(new ConnectionConfig() {
                    ConnectionString = constr, // 修改为你的连接字符串  
                    DbType = DbType.SqlServer, // 根据你的数据库类型设置  
                    IsAutoCloseConnection = true, // 执行完毕后自动关闭连接  
                    InitKeyType = InitKeyType.Attribute // 从实体特性中读取主键和自增列信息  
                });
            } 
        }

    }
}
